package com.gzhryc.shared_device.oem.h5.agent;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceProfitUser;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.place.services.PlaceProfitUserService;

public class PowerCheckUtils {

    /**
     * 检查网点编辑权限
     * @param loginUser
     * @param placeId
     * @return
     * @throws LogicException
     */
    public static boolean checkPlaceEditPower(LoginUser loginUser,Long placeId) throws LogicException {
        if(loginUser != null){
            if(placeId != null) {
                //修改权限
                if (ELoginUserRoleType.Agent.index().equals(loginUser.getRoleType())) {
                    PlacePhysicalStore temp = PlacePhysicalStoreService.self().getById(placeId);
                    if (temp == null) {
                        throw new LogicException("未找到网点信息");
                    }
                    if (temp.getUserId().equals(loginUser.getId())) {
                        return true;
                    }
                } else if (ELoginUserRoleType.Partner.index().equals(loginUser.getRoleType())) {
                    PlaceProfitUser placeProfitUser = PlaceProfitUserService.self().getJoinByKeys(placeId, loginUser.getId());
                    if (placeProfitUser != null) {
                        return true;
                    }
                }
            }else{
                //新增权限
                if (ELoginUserRoleType.Agent.index().equals(loginUser.getRoleType())) {
                    return  true;
                } else if (ELoginUserRoleType.Partner.index().equals(loginUser.getRoleType())) {
                    return  true;
                }
            }
        }
        return false;
    }
}
